package zhongbo.java.air.dictionary.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.dictionary.mapper.AirCraftMapper;
import zhongbo.java.air.dictionary.model.AirCraft;
import zhongbo.java.air.dictionary.service.IAirCraftService;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class AirCraftServiceImpl implements IAirCraftService {
    @Autowired
    private AirCraftMapper airCraftMapper;

    @Override
    public String addAirCraft(AirCraft airCraft) {
        if (airCraft == null) {
            throw new RuntimeException("飞机为空");
        }
        String airId = airCraft.getId();
        if (!isHasTheAddId(airId)) {
            throw new RuntimeException("插入id相同");
        }

        airCraft.setCreateTime(new Date());
        int insert = airCraftMapper.insert(airCraft);
        if (insert > 0) {
            return airCraft.getId(); // 返回新增飞机id
        }
        throw new RuntimeException("新增飞机数据失败");
    }

    @Override
    public AirCraft updateAirCraft(AirCraft airCraft) {
        if (airCraft == null) {
            throw new IllegalArgumentException("飞机信息不能为空");
        }

        airCraft.setUpdateTime(new Date());
        int update = airCraftMapper.updateById(airCraft);
        if (update > 0) {
            return airCraft;
        }
        throw new RuntimeException("修改飞机数据失败");
    }

    @Override
    public int deleteAirCraft(String airId) {
        if (airId == null) {
            throw new RuntimeException("飞机id为空");
        }
        int delete = airCraftMapper.deleteById(airId);
        if (delete > 0) {
            return delete;
        }
        throw new RuntimeException("删除飞机数据失败");
    }

    @Override
    public AirCraft getAirCraftById(String airId) {
        if (airId == null) {
            throw new RuntimeException("飞机id为空");
        }
        return airCraftMapper.selectById(airId);
    }

    @Override
    public List<AirCraft> getAirCraftAll() {
        return airCraftMapper.getAirCraftAll();
    }

    @Override
    public PageVo<AirCraft> page(String aircraftType, int pageNumber, int pageSize) {
        QueryWrapper<AirCraft> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(aircraftType)) {
            queryWrapper.like("aircraft_type",aircraftType);
        }
        IPage<AirCraft> pageParam = new Page<>(pageNumber,pageSize);
        IPage<AirCraft> airCraftIPage = airCraftMapper.selectPage(pageParam,queryWrapper);
        return new PageVo<>(airCraftIPage);
    }
    public boolean isHasTheAddId(String airId) { // 新增时id查重
        List<AirCraft> allAir = airCraftMapper.getAirCraftAll(); // stream取出所有飞机对象
        return allAir.stream().noneMatch(air -> air.getId().equals(airId)); // 传入id与飞机对象的id对比，没有找到相同的，返回true
    }

    // update : 考虑到数据库中有索引uk_icao_code索引类型为UNIQUE，私以为修改不必再加多的东西了
}
